//
//  CustomButton.m
//  事件传递
//
//  Created by slience on 2019/12/9.
//  Copyright © 2019 shehuiren. All rights reserved.
//

#import "CustomButton.h"

@implementation CustomButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    
    CGFloat x1 = point.x;
    CGFloat y1 = point.y;
    
    CGFloat x2 = self.frame.size.width/2.0;
    CGFloat y2 = self.frame.size.height/2.0;
    
    // 当前点击的点 与中心点的距离
    double dis = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    
    if(dis <= self.frame.size.width/2.0){
        
        return YES;
    }else{
        
        return NO;
        
    }
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    
    if(self.userInteractionEnabled == NO|| [self isHidden]|| self.alpha <= 0.01){
        
        return nil;
    }
    
    if ([self pointInside:point withEvent:event]){
        
        __block UIView * hit = nil;
        
        [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            CGPoint vonverPoint = [self convertPoint:point toView:obj];
            
            hit = [obj hitTest:vonverPoint withEvent:event];
            
            if(hit){
                
                *stop = YES;
            }
            
        }];
        
        
        if(hit){
            
            return hit;
        }else{
            
            return self;
        }
    }else{
        
        return nil;
    }
    
    
}

@end
